// 自己改xxx，不然报错
var mysql = require('mysql');
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '123456',
    database : 'yaodian'
  });
connection.connect();

function xxx(sql_str) {
    return new Promise(function (reslove , shibai) {
        // 执行 sql 语句
        connection.query(sql_str, function (error, results, fields) {
            if (error != null) {
                console.log(error)
                var tmp_dic = {
                    error: 1,
                    msg: "执行失败",
                    data: []
                }
                reslove(JSON.stringify(tmp_dic))
            } else {
                var tmp_dic = {
                    error: 0,
                    msg: "执行成功",
                    data: JSON.parse(JSON.stringify(results))
                }
                reslove(JSON.stringify(tmp_dic))
            }
        });
    })
}

const error_info = (msg , error) =>{
    return {
        err_code: 1,
        msg: msg,
        error: error
    }
}
const success_info = (msg , data) =>{
    return {
        err_code: 0,
        msg: msg,
        data: data
    }
}

function get_verify_code(){
    let res = ''
    for (let i = 0; i < 4; i++) {
        res += my_random(0 , 10)
    }
    return res
}

function my_random(min , max){
    return parseInt(Math.random() * (max - min)) + min
}

exports.xxx = xxx

exports.error_info = error_info
exports.success_info = success_info
// exports.get_verify_code = get_verify_code